package com.czl.module_service.viewmodel.assetUse

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.AssetHandleFacilityBean
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.PurchaseOrderBean
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class PurchaseOrderChooseViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 1
    val uc = UiChangeEvent()
    var cardName = ObservableField("")
    var beanList: List<PurchaseOrderBean>? = arrayListOf()

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<ListBean<PurchaseOrderBean>> = SingleLiveEvent()
        val loadSubmitEvent: SingleLiveEvent<MutableList<SortBean>> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getCardListInUseApplication(cardName = cardName.get())
    })

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        getCardListInUseApplication()
    })

    val search: BindingCommand<Void> = BindingCommand(BindingAction {
        showLoading()
        currentPage = 1
        getCardListInUseApplication(cardName = cardName.get())
    })

    val onSubmitClickCommand: BindingCommand<Void> = BindingCommand(BindingAction {
        val selectedList: MutableList<PurchaseOrderBean>? = arrayListOf()
        for (bean in beanList!!) {
            if (bean.selected!!) {
                selectedList?.add(bean)
            }
        }
        if (selectedList != null && selectedList.size > 0) {
            getPurchaseStorageSortList(purchaseId = selectedList[0].purchaseCardId)
        } else {
            showNormalToast("请选择采购单")
        }

    })

    private fun getCardListInUseApplication(
        cardName: String? = null,
        companyId: String? = model.getLoginCompanyId()
    ) {
        model.mTengYunHttpDataSource.getCardListInUseApplication(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "cardName" to cardName,
                    "companyId" to companyId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<PurchaseOrderBean>>>() {
                override fun onResult(t: BaseBean<ListBean<PurchaseOrderBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                        beanList = t.data?.list
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    private fun getPurchaseStorageSortList(
        purchaseId: Int? = null
    ) {
        model.mTengYunHttpDataSource.getPurchaseStorageSortList(
            GsonUtils.toJson(
                mapOf(
                    "purchaseId" to purchaseId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<SortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<SortBean>>) {
                    if (t.state == 0) {
                        if (t.data?.list?.isNotEmpty() == true) {
                            uc.loadSubmitEvent.postValue(t.data?.list as MutableList<SortBean>?)
                        } else {
                            showNormalToast("该采购单下无物品信息")
                        }
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}